var app = getApp(), fetchApi = require("../../../utils/httpPost.js");
import Toast from '../../../component/vant/toast/toast';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        accountList:[],
        pageIndex:1,
        pageSize:20,
        recordList:[],
        hasData:false,
        isLoading:true,
        currentTab: 0,
        duration: 800,
        autoplay: !0,
        interval: 4000,
        indicatorDots: !1,
        circular: !0,
        in_or_outs:[1,0],
        isiphonex: app.globalData.isiphonex,
        account_id:0,
        is_open_cash_bingge:0,
        in_or_out: 1,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      // if(options.is_open_cash_bingge){
      //   this.data.is_open_cash_bingge = parseInt(options.is_open_cash_bingge);
      //   this.setData({
      //     is_open_cash_bingge: this.data.is_open_cash_bingge
      //   })
      // }
    },
    handleChange({ detail }) {
      console.log('handleChange',detail)
      var that = this
      this.data.pageIndex = 1;
      that.data.in_or_out = this.data.in_or_outs[detail.index];
      that.setData({
        currentTab: detail.index,
        loading: false,
        netWork: false,
        isLoading: true,
        hasData:false,
        recordList:[]
      });
      // if(detail.index == 0){
      // }else{
      //   that.onHttpRecordInit()
      // }
      that.onHttpRecord(that.data.account_id)
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.data.pageIndex = 1;
      this.setData({
        hasData:false,
        recordList:[],
      })
      this.onHttpInit();
    },
    /**
     * 获取页面数据的 http请求
     */
    onHttpInit: function () {
        var that = this
        fetchApi.httpPost('/account/detail/bingge', {
          is_person:1
        }, function (res) {
          console.log("账号数据", res)
          if(res.data.code == 0){
              var accountList = res.data.data.accountList;
              that.setData({
                is_open_cash_bingge:res.data.data.is_open_cash_bingge,
                accountList: accountList,
              })
              if(accountList.length > 0){
                that.data.account_id = accountList[0].id;
                that.onHttpRecord(accountList[0].id);
              }

          }
          that.setData({
              netWork: true,
          })
          
        }, function () {
            that.setData({ netWork: false, hasData: false })
        })
    },
    goTixian(e){
        // Toast.success("提现功能暂未开放，敬请期待...");
        var accountId = e.currentTarget.dataset.item.id;
        var money = e.currentTarget.dataset.item.splitMoney;
        wx.navigateTo({
          url: '../tixian/index?id=' + accountId + "&money=" + money,
      })
    },
    changeCurrent: function(t) {
        var e = t.detail.current;
        this.setData({
            current: e
        });
    },
    /**
     *     提现记录  获取页面数据的 http请求
     */
    onHttpRecord: function () {
        var that = this
        fetchApi.httpPost('/account/record/list/bingge', {
          account_id:that.data.account_id,
          pageIndex:that.data.pageIndex,
          pageSize:that.data.pageSize,
          in_or_out: that.data.in_or_out,
        }, function (res) {
        console.log("账号记录数据", res)
        if(res.data.code == 0){
            if (res.data.totalcount > 0) {
                that.data.pageIndex = that.data.pageIndex + 1
                var records = res.data.data;
                if(records.length > 0){
                    for (var index in records) {
                        that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
                    }
                    that.setData({
                        hasData: true,
                        recordList: that.data.recordList
                    })
                }
            }
        }
        that.setData({
            netWork: true,
            isLoading:false,
        })
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        }, function () {
            that.setData({ netWork: false, hasData: false })
        })
    },
    
  /**
   *   获取记录 和 使用记录 获取页面数据的 http请求
   */
  onHttpRecordInit: function () {
    var that = this
    fetchApi.httpPost('/user/integral/recordlist', {
      in_or_out:that.data.in_or_outs[that.data.currentTab],
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
    }, function (res) {
      console.log("积分记录数据", res)
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          that.data.pageIndex += 1
          var records = res.data.data;
          if(records.length > 0){
            for (var index in records) {
              that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
            }
            that.setData({
              hasData: true,
              recordList: that.data.recordList
            })
          }
        }
      }
      that.setData({ netWork: true, isLoading: false, loading: true })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, loading: false })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      isLoading:true,
      recordList:[],
    })
    if(this.data.currentTab == 0){
      this.onHttpRecord()
    }else{
      this.onHttpRecordInit()
    }
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    if(this.data.currentTab == 0){
      this.onHttpRecord()
    }else{
      this.onHttpRecordInit()
    }
  },
})